﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RainEngine.SceneManagement;

namespace RainEngine.SceneNodes.UI
{
    /// <summary>
    /// 在指定屏幕位置绘制一张2D图像
    /// </summary>
    public class UIImage : UISceneNode
    {
        /// <summary>
        /// 纹理的普通状态的源矩形
        /// </summary>
        protected Rectangle? rectSourceOrigin=null;  

        /// <summary>
        /// 根据给定纹理名称创建一个覆盖整个屏幕的UIImage
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属场景</param>
        /// <param name="setTextureName">纹理名称</param>
        public UIImage(RainGE engine, Scene setScene, string setTextureName)
            : this(engine, setScene, setTextureName,null , Vector2.Zero, new Vector2(engine.Graphics.PreferredBackBufferWidth, engine.Graphics.PreferredBackBufferHeight))
        {
            
        }

        /// <summary>
        /// 根据给定纹理名称，位置，大小创建一个位于指定位置和大小的UIImage。内部方法，只能在引擎内部调用。
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属场景</param>        
        /// <param name="setTextureName">纹理名称</param>
        /// <param name="setRectSourceOrigin">图像源矩形</param>
        /// <param name="setPosition">图像的2D屏幕位置</param>
        /// <param name="setSize">图像大小</param>
        /// <param name="setColor">spriteBatch调制颜色</param>
        internal UIImage(RainGE engine, Scene setScene, string setTextureName, Rectangle? setRectSourceOrigin, Vector2 setPosition, Vector2 setSize)
            : base(engine, setScene, setPosition, setTextureName,false )
        {
            if(setRectSourceOrigin.HasValue )
                this.rectSourceOrigin = (Rectangle)setRectSourceOrigin;
            this.size = setSize ;            
            this.TabStop = false;            
        }

        public override int Draw(GameTime gameTime)
        {

            //获取图像淡入淡出的透明颜色
            alphaTextureColor = new Color(Color.R,Color .G,Color.B, Scene.TransitionAlpha);            

            //绘制图像
            RainGE.SpriteBatch.Draw(texture, new Rectangle((int)Position.X, (int)Position.Y, (int)size.X, (int)size.Y), rectSourceOrigin, alphaTextureColor, Rotation, Origin, SpriteEffect, LayerDepth);
            
            return 1;
        }        
    }    
}
